<!--工程部审核进度和检查计划-->
<template>
    <my-dialog :subLoading="subLoading" @on-submit="submitHandle" :readOnly="readOnly">
        <my-header slot="header" project-sub-text="进度检查计划" :project-sub-title="`业主上报日期: ${projectDetail.created_at|| ''}`" :project-name="projectDetail.project_name|| ''"></my-header>
        <div slot="body">
            <widgets title="标段名称" :value="projectDetail.sction_name|| ''" icon="section"></widgets>
            <widgets title="合同工期" :value="projectDetail.contract_time|| ''" icon="contract"></widgets>
            <widgets title="施工单位" :value="projectDetail.construction_organeation|| ''" icon="organization"></widgets>
            <widgets title="项目负责人" :value="projectDetail.project_manager || ''" icon="responsibility"></widgets>
            <widgets title="监理单位" :value="projectDetail.jianli_company|| ''" icon="supervisor"></widgets>
            <widgets title="监理项目负责人" :value="projectDetail.jianli_name || ''" icon="responsibility"></widgets>
            <div style="width: 100%;display: block;height: 50px;float: left;clear: both;line-height: 50px;font-size: 20px;margin-bottom:15px;">施工单位进度图</div>
            <chart :read-only="true" :project="chartProjectData" :is-edit-check="true" :editable="true" :check-data="checkData"></chart>
            <down :files="files"></down>
            <widgets icon="times" class="widget-header" title="工程部审查" :value="completeTimeDiff || ''"></widgets>
            <widgets icon="responsibility" title="审查人员" value="工程部"></widgets>
            <check-box v-model="feasible" :disabled="readOnly" lbl-text="计划是否可行"></check-box>
            <my-textarea v-model="description" :disabled="readOnly" label-text="备注说明" placeholder="请输入备注说明" error-message="请输入备注说明"></my-textarea>
        </div>
    </my-dialog>
</template>
<script>
  import Dialog from "@/components/Dialog";
  import Header from "@/components/Dialog/Header";
  import Widgets from "@/components/Dialog/Widgets";
  import TextArea from "@/components/Dialog/TextArea";
  import Chart from "@/components/Dialog/Chart";
  import down from "@/components/Dialog/Down";
  import CheckBox from "@/components/Dialog/Check";
  import  { mapGetters } from "vuex";
  import moment from 'moment'
  import _ from 'lodash'
  import taskApi from '@/api/task'
  moment.locale('zh-CN')
    export default {
      components: {
        'MyDialog':Dialog,
        'MyHeader':Header,
        Widgets,
        down,
        'MyTextarea':TextArea,
        CheckBox,
        Chart
      },
      data () {
        return {
          readOnly: false,
          subLoading: false,
          feasible: true,
          description: '',
          chartProjectData: [],
          projectDetail: {
            file: []
          },
          planId: '',
          planCreated: '',
          taskDetail: {
            do_task_at: ''
          },
          masterCreated: '',
          checkData:[]
        }
      },
      computed: {
        ...mapGetters([
          'userData'
        ]),
        files () {
          return _.filter(this.projectDetail.file, file => {
            return file.type === 2
          })
        },
        completeTimeDiff () {
          if (this.taskDetail.do_task_at && this.taskDetail.status === 0) {
            const complateTime = moment(this.taskDetail.do_task_at).add(2, 'days')
            const diffHours = complateTime.diff(moment(), 'hours')
            const diffMin = complateTime.subtract(diffHours, 'hours').diff(moment(), 'minutes')
            if (diffMin < 0) {
              return `已超过处理时间${-diffHours}小时${-diffMin}分钟`
            }
            return `距离最后的处理时间剩余:${diffHours}小时${diffMin}分钟`
          }
          if (this.taskDetail.status === 1) {
            return `已处理，上报日期：${this.masterCreated}`
          }
          return ''
        }
      },
      created () {
        this.getProjectInfo()
        this.getProgressPlan()
        this.getTaskInfo()
      },
      methods: {
        getProgressPlan () {
          const projectId=this.$route.params.projectId;

          const taskId = this.$route.params.taskId;

          this.$store.dispatch('getProgressPlan',{
            'token':this.userData.token,
            'pId': projectId,
            'taskId': taskId
          }).then((res) => {
            const branch_project=res.data.branch_project;
            this.planId = res.data.id
            this.planCreated = res.data.created_at
            if (res.data.engineer_examine) {
              this.readOnly = true
              this.description = res.data.engineer_examine.remake
              this.feasible = res.data.engineer_examine.is_approve === 0
              this.masterCreated = res.data.engineer_examine.created_at
            }
            if (branch_project) {

              for(var i=0;i<branch_project.length;i++){

                var tempObj = {
                  projectName:"",
                  items:[]
                };

                var primary=branch_project[i];

                tempObj.projectName=primary.branch_project_name;

                for (var j=0;j<primary.branch_son_project.length;j++){


                  var tempSubObj={
                    subName:"",
                    startDate:'',
                    endDate:''
                  };

                  var secondary=primary.branch_son_project[j];

                  tempSubObj.subName=secondary.branch_son_project_name;
                  tempSubObj.startDate=secondary.construction_begin_at;
                  tempSubObj.endDate=secondary.construction_end_at;

                  tempObj.items.push(tempSubObj);

                }

                this.chartProjectData.push(tempObj);

              }

              /**
               * 检查计划的数据
               */

              for(var i=0;i<branch_project.length;i++){

                for (var j=0;j<branch_project[i].branch_son_project.length;j++){

                  if (branch_project[i].branch_son_project[j].check_plan[0]){

                    var __tempObj={
                      projectName:"",
                      projectSubName:"",
                      checkTime:""
                    };

                    __tempObj.projectName=branch_project[i].branch_project_name;

                    __tempObj.projectSubName=branch_project[i].branch_son_project[j].branch_son_project_name;

                    __tempObj.checkTime=branch_project[i].branch_son_project[j].check_plan[0].plan_at;

                    this.checkData.push(__tempObj);

                  }

                }

              }
            }

          },message => {

            this.$message('错误，请求数据失败,错误信息:'+message);

            this.$router.go(-1);

          }).catch(err =>{

            this.$message('系统错误，请求数据失败，错误信息:'+err);

            this.$router.go(-1);

          });
        },
        getProjectInfo () {
          const projectId = this.$route.params.projectId
          this.$store.dispatch('getSingeProject', {token: this.userData.token, pId: projectId}).then(res => {
            this.projectDetail = res.data
          }, (res) => {
            this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
          }).catch(err => {
            this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
          })
        },
        submitHandle () {
          if (!this.planId) {
            this.$message.error('未获取到进度计划')
            return
          }
          if (!this.description) {
            this.$message.error('请填写备注说明')
            return
          }
          this.subLoading = true
          const projectId = this.$route.params.projectId
          const taskId = this.$route.params.taskId
          taskApi.engineerCheckProgressPlan(this.userData.token, {
            progress_plan_id: this.planId,
            task_id: taskId,
            project_id: projectId,
            remake: this.description,
            is_approve: this.feasible ? 0 : 1,
            users_id: this.projectDetail.users_id
          }).then(res => {
            this.$message.success('提交成功')
            this.subLoading = false
            this.$router.back()
          }, (res) => {
            this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
          }).catch(err => {
            this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
          })
        },
        getTaskInfo () {

debugger;

          const taskId = this.$route.params.taskId
          taskApi.getTaskInfo(this.userData.token, taskId).then(res => {
            const result = res.data
            this.taskDetail = result.data

            console
              .log()

          }).catch(err =>{
            console
              .log(err);
          })
        }
      }
    }
</script>
<style lang="scss" scoped></style>
